/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.tom.social.dal.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import pl.tom.social.dal.SocialDatabaseFacade;
import pl.tom.social.dal.entity.Site;

/**
 *
 * @author Tom
 */
public class EntryIterator implements Iterator {
	private static final Logger logger = Logger.getLogger(EntryIterator.class);
	private SocialDatabaseFacade db = new SocialDatabaseFacade();
	private List<Integer> entryIds = new ArrayList<Integer>();
	private Iterator<Integer> entryIter;
	private Object currentObj;

	public EntryIterator(Site site) {
		entryIds = db.getSiteEntryIds(site);
		entryIter = entryIds.iterator();
	}
	
	@Override
	public boolean hasNext() {
		return entryIter.hasNext();
	}

	@Override
	public Object next() {
		Integer id = entryIter.next();
		try {
			currentObj = db.getSiteEntry(id);
			return currentObj;
		} catch(Exception ex) {
			logger.log(Level.ERROR, ex);
		}
		return null;
	}

	@Override
	public void remove() {
		db.deleteEntity(currentObj);
	}
}
